package com.googlecode.grs.sensor;

import java.awt.Graphics;
import java.util.ArrayList;

import com.googlecode.grs.core.SimulatedSensor;
import com.googlecode.grs.environment.SimulationEnvironment;
import com.googlecode.grs.event.CompassListener;

/**
 * A Compass describes the relative heading of an object in a simulated
 * environment It has a relative position because it is a robot component It
 * draws itself
 * 
 * @author Zhang
 * 
 */

public class Compass extends SimulatedSensor {

	public static final int HEADING = 0;
	public static final int ANGLE = 0;
	private ArrayList<CompassListener> listeners = new ArrayList<CompassListener>();

	/**
	 * TODO (0,0) magic number fix
	 * 
	 * @param env
	 *            The SimulationEnvironment that the Compass exists in
	 * @param heading
	 *            The robot's heading in the SimulationEnvironment
	 */
	public Compass(SimulationEnvironment env) {
		super(env, 0, 0); // robot's relative position
	}

	/**
	 * 
	 * @return the robot's heading
	 */
	private double getHeading() {
		return super.robotHeading();
	}

	/**
	 * draws the Compass in the environment
	 */
	@Override
	public void draw(Graphics g) {

	}

	/**
	 * Adds a CompassListener to be notified on an event
	 * 
	 * @para c a CompassListener
	 */

	public void addCompassListener(CompassListener c) {
		listeners.add(c);
	}

	/**
	 * Removes a CompassListener
	 * 
	 * @param c
	 *            a CompassListener
	 */
	public void removeCompassListener(CompassListener c) {
		listeners.remove(c);
	}

	public void poll() {
		setState(HEADING, getHeading());

	}

	@Override
	public void sensorStateChanged(int id, double prevData, double data) {
		for (CompassListener l : listeners) {
			l.onOrientationChanged(data);
		}
	}

}
